//
//  KCMeDuDaoController.swift
//  MyKC
//
//  Created by yy on 2017/2/23.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//

import UIKit

class KCMeDuDaoController: UITableViewController {
    lazy var titleArray = [["报销","报销","修改密码","关于"],["退出登录"]]
    lazy var imageNameArrray = [["工资_选中","工资_选中","修改密码","关于"],["退出登录"]]
    lazy var VCArray = ["KCReimBurseController","KCChangePwController","KCAboutController"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置界面
        setupUI()
        getData()
    }

    // MARK: -
    // MARK: - 设置界面
    func setupUI() {
        tableView.backgroundColor = UIColor.lightText
        let footView = UIView()
        tableView.tableFooterView = footView
        
        //注册nib文件-------------//
        tableView.register(UINib(nibName: "KCMeNameCell", bundle: nil), forCellReuseIdentifier: "KCMeNameCellId")
        tableView.register(UINib(nibName: "KCMeFunctionCell", bundle: nil), forCellReuseIdentifier: "KCMeFunctionCellId")
        tableView.register(UINib(nibName: "KCDataTongBuCell", bundle: nil), forCellReuseIdentifier: "KCDataTongBuCellId")
    }
    
    // MARK: -
    // MARK: - 获取网络数据---------
    func getData() {
        
    }

}


// MARK: -  tableView-----代理事件
extension KCMeDuDaoController {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 1 ? 4 : 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = getId(indexPath: indexPath)
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        cell.selectionStyle = .none
        setCell(cell: cell, indexPath: indexPath)
        return cell
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return indexPath.section == 0 && indexPath.row == 0 ? 90 : 50
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 0 ? 0.1 : 10
    }
    
    // MARK: -  cell点击事件
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 2 {
            let alertController = UIAlertController(title: "温馨提示", message:"确定要退出程序吗", preferredStyle: .alert)
            let sureAction = UIAlertAction(title: "确定", style: .default, handler: { (UIAlertAction) in
                let login = KCLoginViewController()
                UIApplication.shared.keyWindow?.rootViewController = login
            })
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { (UIAlertAction) in
            })
            alertController.addAction(sureAction)
            alertController.addAction(cancelAction)
            present(alertController, animated: true, completion: nil)
        }
        else if indexPath.section == 1{
            if indexPath.row == 0 {
                print("数据同步被点击了-----")
            }
            if indexPath.row == 1 {
                navigationController?.pushViewController(KCReimurseController(), animated: true)
            }
            if indexPath.row == 2 {
                navigationController?.pushViewController(KCChangePWController(), animated: true)
            }
            if indexPath.row == 3 {
                print("跳转到关于")
                navigationController?.pushViewController(KCAboutController(), animated: true)
            }
        }
    }
    
    func getId(indexPath: IndexPath) -> String {
        if indexPath.section == 0 {
            return "KCMeNameCellId"
        }
        return indexPath.section == 1 && indexPath.row == 0 ? "KCDataTongBuCellId" : "KCMeFunctionCellId"
    }
    
    func setCell(cell: UITableViewCell,indexPath:IndexPath) -> Void {
        if indexPath.section == 0 {
            let cell = cell as! KCMeNameCell
            cell.lbe_name.text = shareAppSetting.userName
            cell.lbe_position.text = "督导"
        }
        else if indexPath.section == 1 && indexPath.row == 0 {
            let cell = cell as! KCDataTongBuCell
            cell.lbe_tongbuNum.text = "1"
        }
        else {
            let cell = cell as! KCMeFunctionCell
            cell.lbe_image.image = UIImage(named: imageNameArrray[indexPath.section - 1][indexPath.row])
            cell.lbe_desc.text = titleArray[indexPath.section - 1][indexPath.row]
        }
    }
}
